// pages/group/group.js
var g = require("../../utils/group")
Page({
  shangchuan(){
    var that = this;
    wx.chooseMedia({
      count: 1,
      success(res) {
          console.log(res.tempFiles)
          var po = res.tempFiles[0].tempFilePath.lastIndexOf(".")
          var ext = res.tempFiles[0].tempFilePath.slice(po)
          wx.cloud.uploadFile({
              cloudPath: that.data.picname + ext,
              filePath: res.tempFiles[0].tempFilePath,
              success(res) {
                  console.log(res.fileID)
                  that.data.imageList.push(res.fileID)
                  that.setData({
                    imageList:that.data.imageList 
                  });
              }
          })
      }
  })
  },
  changeCategory:function(e){
  this.setData({
category:e.currentTarget.id
  });
  },
  shuru:function(e){
   this.setData({
     neirong:e.detail.value
   });
  },
  click(){
  var neirong = this.data.neirong;
  var groups = g.groups;
  var result=[];
  for(let i=0;i<groups.length;i++){
    var data = groups[i];
    if(data.name.match(neirong)){
      result.push(data);
    }
  }
  this.setData({
  result:result
  });
  },
  dianji:function(e){
    
      this.setData({
        index:e.currentTarget.id,
        flag:true
      });
  },
  sousuo(){
   var neirong = this.data.neirong;
   var matches = g.matches;
   var result = [];

   for(let i=0;i<matches.length;i++){
     var data = matches[i];
     if(data.name.match(neirong)){
       result.push(data);
     }
   }
   this.setData({
    result:result
   });
  },cansai:function(e){
   this.setData({
     index:e.currentTarget.id,
     biaozhi:true
   });
  },
  matchname:function(e){
    this.setData({
      matchname:e.detail.value
    });
  },
  picname:function(e){
    this.setData({
      picname:e.detail.value
    });
  },
  delete(){
    console.log(this.data.currentIndex);
    this.data.imageList.splice(this.data.currentIndex,1);
    this.setData({
      imageList:this.data.imageList,
      flag3:false
    }); 
  }, 
  choose:function(e){
    var k = e.currentTarget.id;
    console.log(this.data.imageList[k]);
    this.setData({
      flag3:true,
      currentImage:this.data.imageList[k],
      currentIndex:k
    });
  },
    /**
     * 页面的初始数据
     */
    data: {
      currentIndex:0,
      flag3:false,
      currentImage:null,
      imageList:[],
     picname:null,
     titles:g.list,
     category:2,
     neirong:null,
     result:[],
     index:0,
     flag:false,
     biaozhi:false,
     matchname:null,
     userid:null
    },
    create(){
      var that = this;
      var database = wx.cloud.database();
      var data={
        title:this.data.matchname,
        userid:this.data.userid
      };
      database.collection("tb_match").add({
        data:{
        title:that.data.matchname,
        userid:that.data.userid
        }
      }).then((res)=>{
        console.log("插入一条数据后数据库返回的数据："+res);
        if(res){
          wx.showToast({
            title: '成功插入一条数据',
          })
        }else{
          wx.showToast({ 
            title: '数据插入失败',
          })
        }
      }).catch((err)=>{
        console.log(err);
      });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      var that = this;
       wx.getStorage({
         key:"userid",
         success(res){
           this.setData({
             userid:res.data
           });
         }
       })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})